#include "HumanPlayer.h"


HumanPlayer::HumanPlayer(void)
	:m_bHasSelectChessMan(false)
{
}


HumanPlayer::~HumanPlayer(void)
{
}

void HumanPlayer::ResponseLeftClk( const BoardCoord& currPoint )
{
	//Logger::Instance()->LogToConsole("human response click");
	if(currPoint.x == -1 || m_bHasMove)
		return ;

	int selectChessMan = GlobalMessage::Instance()->m_ChessBoard.GetChessManType(currPoint);

	//it has been not select a chessman,now select one
	if(!m_bHasSelectChessMan)
	{
		//it select it's own chessman
		if( m_iPlayerColor * selectChessMan > 0)
		{
			m_bHasSelectChessMan = true;
			m_CurrSelectChessMan = currPoint;
			GlobalMessage::Instance()->m_bSelectGrid = true;
			GlobalMessage::Instance()->m_CurrentSelectGrid = currPoint;
			//char c = selectChessMan + '0';
			//std::string str("select a chessman : ");
			//str+=c;
			//Logger::Instance()->LogToConsole(str);
		}
	}
	else
	{
		//produce a move information
		m_bHasSelectChessMan = false;
		GlobalMessage::Instance()->m_bSelectGrid = false;
		MakeOneMove(currPoint);
		//if(MakeOneMove(currPoint))
		//	Logger::Instance()->LogToConsole("MakeOneMove Success.");
		//else Logger::Instance()->LogToConsole("MakeOneMove Failed");
	}
}

bool HumanPlayer::MakeOneMove( const BoardCoord& dest )
{
	int chessType = GlobalMessage::Instance()->m_ChessBoard.GetChessManType(m_CurrSelectChessMan);

	int playerType(White);

	if(chessType<0)
		playerType = Black;

	std::vector<ChessManMoveDir>* pMoveTab = ChessManMoveTable::Instance()->GetChessManMoveTab( abs(chessType) );
	std::vector<ChessManMoveDir>::const_iterator iterMoveTab;

	int step;
	//give begin and end coordinate,determine if a direction can make a move
	//if so return true;
	for(iterMoveTab=pMoveTab->begin() ;iterMoveTab!=pMoveTab->end() ;++iterMoveTab)
	{
		if( iterMoveTab->x != 0 )
		{
			step = (dest.x - m_CurrSelectChessMan.x) / iterMoveTab->x;
			//the black player should go a opposite direction 
			step *= playerType;
			if( step < 0 || step > iterMoveTab->iMaxStep)
			{
				continue;
			}
			if(m_CurrSelectChessMan.y + step * (iterMoveTab->y) == dest.y)
			{
				//save the move information and return true
				m_bHasMove = true;
				m_MoveInfo.chessManCoord = m_CurrSelectChessMan;
				m_MoveInfo.step = step;
				m_MoveInfo.x = iterMoveTab->x;
				m_MoveInfo.y = iterMoveTab->y;
				return true;
			}//end if
		}//end if
		else 
		{
			//if (*iterMoveTab).x == 0,so it's y value is non zero
			step = (dest.y - m_CurrSelectChessMan.y) / iterMoveTab->y;
			//the black player should go a opposite direction 
			step *= playerType;
			if( step < 0 || step > iterMoveTab->iMaxStep)
			{
				continue;
			}
			if(m_CurrSelectChessMan.x + step * (iterMoveTab->x) == dest.x)
			{
				//save the move information and return true
				m_MoveInfo.chessManCoord = m_CurrSelectChessMan;
				m_MoveInfo.step = step;
				m_MoveInfo.x = iterMoveTab->x;
				m_MoveInfo.y = iterMoveTab->y;
				m_bHasMove = true;
				return true;
			}//end if
		}//end else

	}//end for

	Logger::Instance()->LogToConsole("Human MoveOneMove failed,no direction");
	return false;
}
